package com.ztgf.sms.api.cms.sms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.ztgf.common.bo.HeadParamsBO;
import com.ztgf.common.constant.CommonParameter;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.sms.models.bo.SmsInfoBatchBO;
import com.ztgf.sms.models.bo.SmsInfoQueryBO;
import com.ztgf.sms.models.bo.SmsInfoSaveCaptchaBO;
import com.ztgf.sms.models.dto.sms.SmsInfoDTO;
import com.ztgf.sms.models.service.ISmsInfoService;
import com.ztgf.sms.models.vo.SmsInfoVO;
import com.ztgf.sys.models.bo.SysNoticeQueryBO;
import com.ztgf.sys.models.constant.SysConstant;
import com.ztgf.sys.models.dto.SysDictDTO;
import com.ztgf.sys.models.service.ISysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * 短信对内接口类
 *
 * @author zhanghaulang
 * @Date 2020-05-26
 */
@RestController
@RequestMapping("/cms/sms")
@Slf4j
@Api(value = "SmsInfoController",tags = "短信服务接口")
public class SmsInfoController {
    @DubboReference
    private ISmsInfoService smsInfoService;
    @DubboReference
    private ISysDictService sysDictService;

    @ApiOperation(value = "批量发送短信(后台推送)，对接玄武科技或成都创信",notes = "批量发送短信(后台推送)，对接玄武科技或成都创信")
    @PostMapping(value = "/saveBatchByType")
    public Result<?> saveBatchByType(SmsInfoBatchBO bo) throws Exception{
        if(bo != null){
            HeadParamsBO headParamsBO = CommonParameter.getHeadParamsBO();
            smsInfoService.saveBatchByType(bo,headParamsBO.getSessionId(),headParamsBO.getSource().toString());
            return new Result<>();
        }else {
            return new Result<> (CodeEnum.FAIL_100002);
        }
    }

    @ApiOperation(value = "短信详情",notes = "短信详情")
    @PostMapping("/queryPageListByNoticeId")
    public Result<Page<SmsInfoDTO>> queryPageListByNoticeId(SysNoticeQueryBO sysNoticeQueryBO){
        Page<SmsInfoDTO> page = smsInfoService.queryPageListByNoticeId(sysNoticeQueryBO);
        return new Result<>(page);
    }
    @ApiOperation(value = "批量发送短信(后台推送)，对接玄武科技或成都创信",notes = "批量发送短信(后台推送)，对接玄武科技或成都创信")
    @PostMapping(value = "/saveBatch")
    public Result<?> saveBatch(SmsInfoBatchBO bo) throws Exception{
        HeadParamsBO headParamsBO = CommonParameter.getHeadParamsBO();
        smsInfoService.saveBatch(bo,headParamsBO.getSessionId(),headParamsBO.getSource().toString());
        return new Result<>();
    }

    @ApiOperation(value = "发送验证码(对接玄武科技或成都创信) ",notes = "发送验证码(对接玄武科技或成都创信)")
    @PostMapping(value = "/saveCaptcha")
    public Result<?> saveCaptcha( SmsInfoSaveCaptchaBO smsInfoSaveCaptchaBO) throws Exception{
        smsInfoService.saveCaptcha(smsInfoSaveCaptchaBO);
        return new Result<>();
    }

    @ApiOperation(value = "删除短信消息",notes = "删除短信消息")
    @ApiImplicitParam(name = "id",value = "短信信息表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result<?> delete(@RequestParam("id") Long id){
        boolean flag = smsInfoService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除短信消息",notes = "批量删除短信消息")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result<?> deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        smsInfoService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @ApiOperation(value = "后台推送短信列表",notes = "后台推送短信列表")
    @PostMapping(value = "/queryPageList")
    public Result <Page<SmsInfoVO>> queryPageList(SmsInfoQueryBO bo){
        return new Result<>(smsInfoService.queryPageList(bo));
    }

    @ApiOperation(value = "短信查询",notes = "短信查询")
    @PostMapping("/queryChildUserPageList")
    public Result<Page<SmsInfoDTO>> queryChildUserPageList(SmsInfoQueryBO bo){
        Page<SmsInfoDTO> page = smsInfoService.queryAllPageList(bo);
        return new Result<>(page);
    }

    @ApiOperation(value = "获取通道资费(成都创信和玄武)",notes = "获取通道资费(成都创信和玄武)")
    @PostMapping(value = "/getAisleInfo")
    public Result<?> getAisleInfo() throws Exception{
        return smsInfoService.getAisleInfo();
    }

    @ApiOperation(value = "更改中铁短信通道",notes = "更改中铁短信通道")
    @PostMapping(value = "/updateSmsAisle")
    public Result<?> updateSmsAisle(String aisle){
        if (StringUtils.isEmpty(aisle)){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        String name = SysConstant.SMS_USE_TYPE;
        SysDictDTO dictDTO = new SysDictDTO();
        dictDTO.setValue(aisle);
        Wrapper<SysDictDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("name", name);
        boolean flag = sysDictService.update(dictDTO, wrapper);
        if (flag){
            return new Result<>();
        }else {
            return new Result<>(CodeEnum.FAIL);
        }
    }

}
